﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseBindAppend : frmBase
    {
        public frmMerchandiseBindAppend()
        {
            InitializeComponent();
        }

        MerchandiseBindBLL oMerchandiseBindBLL = new MerchandiseBindBLL();

        private void frmMerchandiseBindAppend_Load(object sender, EventArgs e)
        {
            Thread LoadUnitThread = new Thread(new ThreadStart(BindUnitName));
            LoadUnitThread.IsBackground = true;
            LoadUnitThread.Priority = ThreadPriority.Highest;
            LoadUnitThread.Start();
        }

        private void BindUnitName()
        {
            List<MerchandiseUnitInfo> oMerchandiseUnitInfo = new MerchandiseUnitBLL().GetAllMerchandiseUnitList();
            foreach (MerchandiseUnitInfo item in oMerchandiseUnitInfo)
            {
                AsyncMethod.ControlsEndueValueEventHandler OnSet = new AsyncMethod.ControlsEndueValueEventHandler(this.OnComboBoxEndueValueEvent);
                this.BeginInvoke(OnSet, new object(), new AsyncMethod.EndueValueEventArgs(cbbUnit, item, OnSet));
            }
            try
            {
                AsyncMethod.ControlsEndueValueEventHandler OnSetIndex = new AsyncMethod.ControlsEndueValueEventHandler(base.OnComboBoxEndueValueEvent);
                base.BeginInvoke(OnSetIndex, new object(), new AsyncMethod.EndueValueEventArgs(cbbUnit, 0, OnSetIndex));
            }
            catch { log.Write("商品单位为0"); }
        }

        protected override void OnComboBoxEndueValueEvent(object sender, AsyncMethod.EndueValueEventArgs e)
        {
            ComboBox cbbTmp = (ComboBox)e.ControlName;
            if (cbbTmp.InvokeRequired) this.BeginInvoke(e.OnControlEndueValue, new object(), e);
            else cbbTmp.Items.Add(e.SetValue);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            lblMsg.Visible = true;
            string strBindName = txtBindName.Text.Trim();
            int UintID = ((MerchandiseUnitInfo)cbbUnit.SelectedItem).UnitID;
            decimal mPrice = Convert.ToDecimal(txtBindSellPrice.Text.Trim());
            if (string.IsNullOrEmpty(strBindName))
            {
                lblMsg.Text = "请输入名称";
                this.txtBindName.SelectAll();
                this.txtBindName.Focus();
                return;
            }
            if (mPrice <= 0m)
            {
                lblMsg.Text = "请输入有效的价格";
                this.txtBindSellPrice.SelectAll();
                this.txtBindSellPrice.Focus();
                return;
            }
            if (oMerchandiseBindBLL.MerchandiseBindAppend(strBindName, UintID, mPrice))
            {
                MessageBox.Show("添加成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("添加失败，可能原因：" + txtBindName.Text + "已经存在");
                this.txtBindName.SelectAll();
                this.txtBindName.Focus();
            }
        }
    }
}
